package it.trekker.activities;

import it.trekker.R;
import it.trekker.dialogs.NewExcursionDialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements NewExcursionDialog.INewExcursion {

	private static final String TAG = MainActivity.class.getSimpleName();

	private Button listExcursions_bt = null;
	private Button newExcursion_bt = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_layout);

		listExcursions_bt = (Button) findViewById(R.id.main_listexcursion_bt);
		newExcursion_bt = (Button) findViewById(R.id.main_newexcursion_bt);

		listExcursions_bt.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				lounchActivity(ExcursionsListViewActivity.class);
			}
		});

		newExcursion_bt.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_NEWEXCURSION);
			}
		});
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		Log.d(TAG, "onConfigurationChanged");
	}

	
	

	//*****************************************************************//
	//************************** Option menu **************************//
	//*****************************************************************//
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_activity_menu, menu);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		
			case R.id.settings:
				Intent intent = new Intent(this, TrekkerPreferenceActivity.class);
				startActivity(intent);
				return true;
				
			case R.id.upnp:
				Intent intent2 = new Intent(this, UPnPActivity.class);
				startActivity(intent2);
				return true;
				
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	
	
	//*****************************************************************//
	//*********************** Utility functions ***********************//
	//*****************************************************************//

	private void lounchActivity(Class activity) {
		Intent intent = new Intent(this, activity);
		startActivity(intent);
	}

	@Override
	public void createNewExcursion(String name) {
		Intent intent = new Intent(this, ExcursionMainActivity.class);
		intent.putExtra("excursion_name", name);
		startActivity(intent);
	} 

	
	


	//***********************************************************//
	//************************* Dialogs *************************//
	//***********************************************************//


	private static final int DIALOG_NEWEXCURSION = 0;

	private void showCreateNewExcursionDialog() {
		showDialog(DIALOG_NEWEXCURSION);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;

		switch(id) {
		case DIALOG_NEWEXCURSION:
			dialog = createNewExcursionDialog();
			break;

		default:
			dialog = null;
		}

		return dialog;
	}

	private NewExcursionDialog createNewExcursionDialog() {
		return new NewExcursionDialog(this);
	}

	@Override
	public Context getContext() {
		return this;
	}
}
